//
//  AccessoryPreview.m
//  MISCloud
//
//  Created by goldeney on 13-9-18.
//  Copyright (c) 2013年 goldeneye. All rights reserved.
//

#import "GEAccessoryPreview.h"
#import "GEAccessoryPreviewCell.h"
#import "DownLoadHandle.h"
#import "NSDictionary+DataFormat.h"

@interface GEAccessoryPreview()

@property (strong, nonatomic) NSMutableArray *dataArray;
@property (strong, nonatomic) MLTableAlert *alert;
@property (strong, nonatomic) DownLoadHandle *downLoad;

@property (assign, nonatomic) BOOL isCustomKey;
@property (strong, nonatomic) NSString *nameKey;
@property (strong, nonatomic) NSString *path;

@end

@implementation GEAccessoryPreview
@synthesize dataArray = _dataArray;
@synthesize alert = _alert;

- (id)init
{
    self = [super init];
    if (self) {
        self.dataArray = [NSMutableArray array];
    }
    return self;
}

-(void)showAccessoryTableAlert:(NSArray*)fileArray nameKey:(NSString*)nameKey pathKey:(NSString*)path
{
    self.nameKey = nameKey;
    self.path = path;
    self.isCustomKey = YES;
    [self showAccessoryTableAlert:fileArray];
}


#pragma mark -
#pragma mark 附件查看对话框
-(void)showAccessoryTableAlert:(NSArray*)fileArray
{
    //数据添加
    if ([fileArray isKindOfClass:[NSArray class]])
    if(fileArray && fileArray.count > 0)
    [self.dataArray addObjectsFromArray:fileArray];
    
	// 创建对话框
    if (!_alert) {
        _alert = [MLTableAlert tableAlertWithTitle:@"附件列表" cancelButtonTitle:@"取消" numberOfRows:^NSInteger (NSInteger section){
            return [_dataArray count];
        } andCells:^UITableViewCell* (MLTableAlert *anAlert, NSIndexPath *indexPath) {
            GEAccessoryPreviewCell *cell = [anAlert.table dequeueReusableCellWithIdentifier:@"AccessoryPreviewCell"];
            if (cell == nil) {
               cell = [[GEAccessoryPreviewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AccessoryPreviewCell"];
            }
            
            NSMutableDictionary *dataDict = [_dataArray objectAtIndex:indexPath.row];
            if (self.isCustomKey) cell.fileNameLabel.text = [dataDict objectForKey:self.nameKey];
            else cell.fileNameLabel.text = [dataDict objectForKey:@"FJName"];
            cell.downButton.tag = indexPath.row;
            [cell.downButton addTarget:self action:@selector(downSelector:) forControlEvents:UIControlEventTouchUpInside];
            
            return cell;
        }];
        _alert.height = 400;
        _alert.cellHeight = 56;
        _alert.notCanDownCell = YES;
        
        // 显示提示框，并获取附件数据
        [_alert show];
    }
}

#pragma mark -
#pragma mark Button Event
- (void) downSelector:(id)selector
{
    UIButton *selectorButton = selector;
    NSMutableDictionary *mDictionary =[_dataArray objectAtIndex:selectorButton.tag];
    
    NSString *fileUrl = nil;
    if (self.isCustomKey) fileUrl = [mDictionary objectForKey:self.path];
    else fileUrl = [mDictionary objectForKey:@"FJPath"];
    
    NSString *fjName = nil;
    if (self.isCustomKey) fjName = [mDictionary objectForKey:self.nameKey];
    else fjName = [mDictionary objectForKey:@"FJName"];
    
    //创建连接，下载文件（需要strong类型）
    self.downLoad = [[DownLoadHandle alloc] init];
    [self.downLoad startDownExternalFile:fileUrl fileName:fjName];
}


@end
